#include "RecordThread.h"
#include <QApplication>
#include <QDebug>

RecordThread::RecordThread(QObject *parent)
    :QThread(parent)
{
    m_bStoped = false;
    m_pixByteArray.clear();

    /*
avi_t* AVI_open_output_file(char * filename);
void AVI_set_video(avi_t *AVI, int width, int height, double fps, char *compressor);
void AVI_set_audio(avi_t *AVI, int channels, long rate, int bits, int format, long mp3rate);
int  AVI_write_frame(avi_t *AVI, char *data, long bytes, int keyframe);
*/
}

void RecordThread::setWidgetSize(const QSize &size)
{
    m_size = size;
}

void RecordThread::stopThread()
{
    m_bStoped = true;
}

void RecordThread::slotRcvData(const QByteArray &data)
{
    if(data.isEmpty())
    {
        return;
    }

    m_pixByteArray = data;
}

void RecordThread::run()
{
    QString strAppDir = QApplication::applicationDirPath() + "/test.avi";

    avi_t *FD = AVI_open_output_file(strAppDir.toUtf8().data());
    AVI_set_video(FD, m_size.width(), m_size.height(), 20, QByteArray("MJPG").data());

    while(!m_bStoped)
    {
        if(!m_pixByteArray.isEmpty())
        {
            int iRet = AVI_write_frame(FD, m_pixByteArray.data(), m_pixByteArray.length(), 1);
            qDebug() << iRet;
            m_pixByteArray.clear();
        }
    }
    AVI_close(FD);

    this->exec();
    this->deleteLater();
}
